package com.xiaomi.service.impl;

import com.xiaomi.dao.AddressDao;
import com.xiaomi.dao.impl.AddressDaoImpl;
import com.xiaomi.entity.Address;
import com.xiaomi.service.AddressService;

import java.util.List;

/**
 * TODO
 *
 * @author jy
 * @date 2021/5/25
 */
public class AddressServiceImpl implements AddressService {

    private final AddressDao addressDao = new AddressDaoImpl();

    @Override
    public List<Address> showAddressesByUid(int uid) {
        try {
            return addressDao.showAddressesByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int addAddress(Address address) {
        try {
            return addressDao.addAddress(address);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int delete(String aid) {
        try {
            if (aid != null && !"".equals(aid)) {
                return addressDao.delete(Integer.parseInt(aid));
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int setDefault(int uid, String aid) {
        try {
            if (aid != null && !"".equals(aid)) {
                //先把该用户所有默认地址level设置为0
                int count1 = addressDao.setDefaultByUid(uid);
                //再将aid对应地址level设置为1
                int count2 = addressDao.setDefaultByAid(Integer.parseInt(aid));
                return count1 + count2;
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int update(Address address) {
        try {
            return addressDao.update(address);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

}
